﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Text;
using System.Collections;
using System.Xml;
using System.Web.Services;
using FileManager;

public partial class Comod_StockManage : ATPages
{

    ZSql zsql = new ZSql();

    Function f = new Function();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            hfxs.Value = Request.QueryString["moduid"].ToString();
            lblwz.Text = f.getModuleidAdd(Request.QueryString["moduid"]);
            ComoditiesType.Text=Request.QueryString["ComoditiesType"];
            Specifications.Text=Request.QueryString["Specifications"];
            modulbh.Text=Request.QueryString["modulbh"];
            ComoditiesName.Text=Request.QueryString["ComoditiesName"];
            Brand.Text = Request.QueryString["Brand"];
            FactNum.SelectedValue = Request.QueryString["FactNum"];
            Warehouse.SelectedValue = Request.QueryString["Warehouse"];
            initData();
            ddlComodName();//仓库

        }
    }

    private void initData()
    {
        //数据邦定
        //数据邦定
        string search = "";
//        string searchWarehouse = "";
        string SqlWarehouse = "";
        string SqlSalesWarehouse = "";
        if (!string.IsNullOrEmpty(Request.QueryString["Warehouse"]))
        {
            search += "&Warehouse=" + Request.QueryString["Warehouse"];
            SqlWarehouse += " and CS_Stock.WarehouseId ='" + Request.QueryString["Warehouse"] + "' ";
            SqlSalesWarehouse += " and WarehouseId ='" + Request.QueryString["Warehouse"] + "' ";
        }
        //SQL语句
        string sql = "SELECT * FROM (select a.TypeId,s.SpecificationsId,s.AuditState,s.comodid, s.Specifications,s.SpecificationsOrder,s.UserName,s.usTime,a.ComoditiesType,b.Brand,c.ComoditiesName,s.modulbh, (SELECT COUNT(1)  FROM CS_Stock  WHERE CS_Stock.ModelId = s.SpecificationsId" + SqlWarehouse + ") AS countStock,ISNULL ((SELECT SUM(ProcNum) FROM CS_Stock  WHERE CS_Stock.ModelId = s.SpecificationsId" + SqlWarehouse + "), 0) AS ProcNum,ISNULL((SELECT sum(number) FROM CS_Sales WHERE comodBarcode = s.modulbh and isDel=0 " + SqlSalesWarehouse + "),0) as SalesNum,(ISNULL ((SELECT SUM(ProcNum) FROM CS_Stock  WHERE CS_Stock.ModelId = s.SpecificationsId" + SqlWarehouse + "), 0) -ISNULL((SELECT sum(number) FROM CS_Sales WHERE comodBarcode = s.modulbh and isDel=0" + SqlSalesWarehouse + "),0)) as FactNum from ComoditiesType as a ,Brand as b,ComoditiesName as c,Specifications as s  where a.TypeId=s.TypeId and b.BrandId=s.BrandId and c.ComoditiesNameId=s.ComoditiesNameId ) tblStock where 1=1";
        if (!string.IsNullOrEmpty(Request.QueryString["ComoditiesType"]))
        {
            search += "&ComoditiesType=" + ComoditiesType.Text;
            sql += " and ComoditiesType like '%" + Common.FormatParameter(Request.QueryString["ComoditiesType"]) + "%'";
        }
        if (!string.IsNullOrEmpty(Request.QueryString["Specifications"]))
        {
            search += "&Specifications=" + Specifications.Text;
            sql += " and Specifications like '%" + Common.FormatParameter(Request.QueryString["Specifications"]) + "%'";
        }

        if (!string.IsNullOrEmpty(Request.QueryString["modulbh"]))
        {
            search += "&modulbh=" + modulbh.Text;
            sql += " and modulbh like '%" + Common.FormatParameter(Request.QueryString["modulbh"]) + "%'";
        }

        if (!string.IsNullOrEmpty(Request.QueryString["ComoditiesName"]))
        {
            search += "&ComoditiesName=" + ComoditiesName.Text;
            sql += " and ComoditiesName like '%" + Common.FormatParameter(Request.QueryString["ComoditiesName"]) + "%' ";
        }

        if (!string.IsNullOrEmpty(Request.QueryString["Brand"]))
        {
            search += "&Brand=" + Brand.Text;
            sql += "  and Brand like '%" + Common.FormatParameter(Request.QueryString["Brand"]) + "%' ";
        }

        if (!string.IsNullOrEmpty(Request.QueryString["FactNum"]))
        {
            search += "&FactNum=" + Request.QueryString["FactNum"];
            sql += "  and FactNum" + Request.QueryString["FactNum"];
        }
        sql += "ORDER BY TypeId DESC";

        PageNavigator1.Url = "../Comod/StockManage.aspx?moduid=" + hfxs.Value + search;
        PageNavigator1.Total = 1;
        PageNavigator1.Query = sql;
    }

    protected void btnAdd_Click(object sender, EventArgs e)//生成excel文件
    {
        Response.Clear();
        Response.Buffer=true;
        Response.Charset = "GB2312";
        Response.AppendHeader("Content-Disposition", "attachment;filename=PriceManage" + DateTime.Now.Date.ToString("yyyyMMdd") + ".xls");
        // 如果设置为 GetEncoding("GB2312");导出的文件将会出现乱码！！！
        Response.ContentEncoding = System.Text.Encoding.UTF7;
        Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。 
        System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
        //  turn off paging 

        StringWriter sw = new StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);
        this.GvData.RenderControl(htw);
        string strHtml = sw.ToString().Trim();
        Response.Output.Write(strHtml);
        Response.Flush();
        Response.End();
        // turn the paging on again 

    }
    //添加一个空方法,防止   GridView”的控件“GridView1”必须放在具有 runat=server 的窗体标记内错误
    public override void VerifyRenderingInServerForm(Control control)
    {
      // Confirms that an HtmlForm. control is rendered for
    }
    //------------------------------生成excel END----------------------------------------

    protected void lnkbtnFlag_Click(object sender, EventArgs e)
    {
        string ComodBarcode = (sender as LinkButton).CommandArgument.ToString();
        Response.Redirect("../Sales/SaleDitails.aspx?moduid=" + Request.QueryString["moduid"] + "&ComodBarcode=" + ComodBarcode);

    }

    protected void lnkbtnEdit_Click(object sender, EventArgs e)
    {
        string ComodBarcode = (sender as LinkButton).CommandArgument.ToString();
        Response.Redirect("StorageManage.aspx?moduid=" + Request.QueryString["moduid"] + "&ComodBarcode=" + ComodBarcode);

    }



    protected void btnSelec_Click(object sender, EventArgs e)
    {
        string search = "";
        if (!string.IsNullOrEmpty(ComoditiesType.Text))
        {
            search += "&ComoditiesType=" + ComoditiesType.Text;
        }
        if (!string.IsNullOrEmpty(Specifications.Text))
        {
            search += "&Specifications=" +Specifications.Text;
        }

        if (!string.IsNullOrEmpty(modulbh.Text))
        {
            search += "&modulbh=" + modulbh.Text;
        }

        if (!string.IsNullOrEmpty(ComoditiesName.Text))
        {
            search += "&ComoditiesName=" + ComoditiesName.Text;
        }

        if (!string.IsNullOrEmpty(Brand.Text))
        {
            search += "&Brand=" +Brand.Text;
        }
        if (!string.IsNullOrEmpty(FactNum.Text))
        {
            search += "&FactNum=" + FactNum.Text;
        }
        if (!string.IsNullOrEmpty(Warehouse.SelectedValue.Trim()))
        {
            search += "&Warehouse=" + Common.FormatParameter(Warehouse.SelectedValue.Trim());
        }
        Response.Redirect("StockManage.aspx?moduid=" + hfxs.Value + search);

    }

    protected void btnallflag_Click(object sender, EventArgs e)
    {
        ZSql allflag = new ZSql();
        int n = 0;
        if (this.GvData.Rows.Count > 0)
        {
            for (int i = 0; i < GvData.Rows.Count; i++)
            {
                CheckBox ck = GvData.Rows[i].Cells[10].FindControl("chkDelete") as CheckBox;
                if (ck.Checked)
                {
                    int flag = 1;
                    allflag.Open("update Specifications set AuditState=" + flag + " where SpecificationsId=" + this.GvData.Rows[i].Cells[0].Text);
                    ClientScript.RegisterStartupScript(typeof(Page), "aa", "alert('审核成功');", true);
                }
                else
                {
                    n++;
                }

                if (n == this.GvData.Rows.Count)
                {

                    ClientScript.RegisterStartupScript(typeof(Page), "aa", "alert('至少应选择一项')", true);
                }


            }
        }
    }
    protected void btndel_Click(object sender, EventArgs e)
    {
        ZSql del = new ZSql();
        int n = 0;
        if (this.GvData.Rows.Count > 0)
        {
            for (int i = 0; i < GvData.Rows.Count; i++)
            {
                CheckBox ck = GvData.Rows[i].Cells[11].FindControl("chkDelete") as CheckBox;
                if (ck.Checked)
                {

                    del.Open("delete Specifications  where SpecificationsId=" + this.GvData.Rows[i].Cells[0].Text);
                    ClientScript.RegisterStartupScript(typeof(Page), "aa", "alert('删除成功');", true);
                }
                else
                {
                    n++;
                }

                if (n == this.GvData.Rows.Count)
                {

                    ClientScript.RegisterStartupScript(typeof(Page), "aa", "alert('至少应选择一项')", true);
                }

            }
        }
    }
    protected void GvData_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        f.checkLevels(Request.Cookies["user"].Values["roleid"], hfxs.Value, 12, GvData, btndel, btnallflag, btnAdd);
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //删除是否可用
            string delDisabled = DataBinder.Eval(e.Row.DataItem, "countStock").ToString();
            if (Convert.ToInt32(delDisabled) > 0)
            {
                (e.Row.Cells[10].FindControl("chkDelete") as CheckBox).Enabled = false;
            }
        }
    }

    protected void ddlComodName()
    {
        ZSql ware = new ZSql();
        ware.Open("select warehouseid,warehousename from CS_Warehouse ");
        if (ware.m_table.Rows.Count > 0)
        {
            Warehouse.DataSource = ware.m_table;
            Warehouse.DataTextField = "warehousename";
            Warehouse.DataValueField = "warehouseid";
            Warehouse.DataBind();
            Warehouse.Items.Insert(0, new ListItem("所有仓库", ""));

            ware.Close();
        }

    }
}
